﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBControl;

namespace KSGJ.AjaxCommand
{
    /// <summary>
    /// savesubuser 的摘要说明
    /// </summary>
    public class savesubuser : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int errcount = 0;
            string subuseridlist = context.Request.Form["idlist"];
            string subnamelist = context.Request.Form["subname"];
            string maxmoneylist = context.Request.Form["maxmoneylist"];
            string isallpromotion = context.Request.Form["allpromotion"];

            string[] idlist = subuseridlist.Split(',');
            string[] namelist = subnamelist.Split(',');
            string[] maxlist = maxmoneylist.Split(',');
            string[] isalllist = isallpromotion.Split(',');
            SqlDbHelper sdh = new SqlDbHelper();

            string deletesql = "DELETE FROM UserSession WHERE ParentNick='" + context.Session["usNick"].ToString() + "'";
            sdh.ExecuteNonQuery(deletesql);

            for (int i = 0; i < idlist.Length; i++)
            {
                string insertsql = "INSERT INTO UserSession(UsID,UsNick,SessionKey,ParentNick,maxmoney,IsAllPromotion) VALUES('" + idlist[i] + "','" + namelist[i] + "','" + context.Session["sessionKey"].ToString() + "','" + context.Session["usNick"].ToString() + "'," + maxlist[i] + "," + isalllist[i] + ")";
                if (sdh.ExecuteNonQuery(insertsql) <= 0)
                {
                    errcount += 1;
                }
            }

            if (errcount > 0)
            {
                context.Response.Write("有"+ errcount +"项保存失败，请检查后再重新保存!");
            }
            else
            {
                context.Response.Write("全部子账户保存成功!");
            }

            //context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}